package ckw.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;

import ckw.database.model.Election;
import core.cards.IFormBackend;
import core.cards.election.gui.CmpElectionWizardPage;
import core.cards.election.model.ElectionBackend;
import core.utils.Converter;

public class WzdPgAddEditElectionCards extends ModelValidationWizardPage {

	private boolean addElection;
	private Election election;

	private CmpElectionWizardPage cmpElectionWizardPage;

	/**
	 * Create the wizard.
	 */
	public WzdPgAddEditElectionCards(boolean addElection, Election election) {
		super("WzdPgAddEditElectionCards");

		this.addElection = addElection;
		this.election = election;

		if (addElection) {
			setTitle("Add Elections Cards");
		} else {
			setTitle("Edit Elections Cards");
		}

		setDescription("Set Elections Cards");
	}

	/**
	 * Create contents of the wizard.
	 * 
	 * @param parent
	 */
	public void createControl(Composite parent) {
		cmpElectionWizardPage = new CmpElectionWizardPage(parent, SWT.NONE);
		setControl(cmpElectionWizardPage);

		initValues();
	}

	private void initValues() {
		if (addElection) {
			// setPageComplete(false);
		} else {
			ElectionBackend formBackend = (ElectionBackend) Converter.xmlToObject(election.getCards());
			cmpElectionWizardPage.setFormBackend(formBackend);
		}

	}

	@Override
	public void validate() {
		IFormBackend formBackend = cmpElectionWizardPage.getFormBackend();

		if (!formBackend.isValid()) {
			setErrorState(formBackend.getValidationMessage());
			return;
		}
		
		clearErrorState();
	}

	@Override
	protected void update() {
		IFormBackend formBackend = cmpElectionWizardPage.getFormBackend();
		election.setCards(Converter.objectToXML(formBackend));
	}
}
